<?php

class worker_type_class {

    public $TABLE_NAME = 'Worker_type';
    public $TABLE_COLUMNS = array(
        'id' => 'Worker_type_id',
        'type_name' => 'Worker_type_name'
    );

    function __construct($dblink) {
        if ($dblink == NULL || !$dblink) {
            echo 'You need to specify a database valid link';
            return NULL;
        }
        $this->DB_LINK = $dblink;
    }

    function find_by($Columns, $Values, $Limit = NULL, $Offset = NULL, $Newest = false) {
        $select_sql = sprintf("
            SELECT
                *
            FROM
                `%s`
            WHERE
               ", $this->TABLE_NAME
        );
        $error = false;

        if (is_array($Columns) && is_array($Values)) {
            if (( $condition_number = count($Columns) ) && ( count($Columns) == count($Values) )) {
                for ($i = 0; $i < $condition_number; $i++) {
                    if ($i > 0) {
                        $select_sql .= " AND ";
                    }
                    $select_sql .= sprintf(
                            " `%s`=?  ", $this->TABLE_COLUMNS[$Columns[$i]]
                    );
                }
                if ($Newest) {
                    $select_sql .= sprintf("ORDER BY `%s` DESC", $this->TABLE_COLUMNS['id']);
                }
                if ($Limit > 0) {
                    if ($Offset > 0) {
                        $select_sql .= sprintf(" LIMIT %d,%d", $Offset, $Limit);
                    } else {
                        $select_sql .= sprintf(" LIMIT %d", $Limit);
                    }
                }
                $select_result = $this->DB_LINK->Execute($select_sql, $Values);
            } else {
                $error = true;
            }
        } elseif (!is_array($Columns) && !is_array($Values)) {
            $select_sql = sprintf("
                SELECT
                    *
                FROM
                    `%s`
                WHERE
                    `%s`=? ", $this->TABLE_NAME, $this->TABLE_COLUMNS[$Columns]
            );
            if ($Newest) {
                $select_sql .= sprintf("ORDER BY `%s` DESC", $this->TABLE_COLUMNS['id']);
            }
            if ($Limit > 0) {
                if ($Offset > 0) {
                    $select_sql .= sprintf(" LIMIT %d,%d", $Offset, $Limit);
                } else {
                    $select_sql .= sprintf(" LIMIT %d", $Limit);
                }
            }
            $select_result = $this->DB_LINK->Execute($select_sql, array($Values));
        } else {
            $error = true;
        }

        if ($select_result && ( $error == false )) {
            return $select_result->GetRows();
        } else {
            return array();
        }
    }

    function modify_data_for_id($Id, $Data) {
        if ($Id <= 0) {
            return false;
        }

        $modify_user_data_sql = sprintf("UPDATE
                                            `%s`
                                          SET
                                          ", $this->TABLE_NAME);

        $values = array();
        foreach ($Data as $key => $c) {
            $values[] = $c;
            $modify_user_data_sql .= sprintf(" `%s` = ? , ", $this->TABLE_COLUMNS[$key]);
        }
        $modify_user_data_sql = substr($modify_user_data_sql, 0, -2);
        $modify_user_data_sql .= sprintf("WHERE
                                            `%s`= ? ", $this->TABLE_COLUMNS['id']
        );
        $values[] = $Id;

        $modify_result = $this->DB_LINK->Execute($modify_user_data_sql, $values);
        if (!$modify_result) {
            return $this->DB_LINK->ErrorMsg();
        } else {
            return TRUE;
        }

        return TRUE;
    }

    function get_all() {
        $get_all_sql = sprintf("SELECT * FROM `%s`", $this->TABLE_NAME);
        $get_all_result = $this->DB_LINK->GetAll($get_all_sql);
        if ($get_all_result) {
            return $get_all_result;
        } else {
            return false;
        }
    }
    
    function add( $Data )
    {
        $insert_sql = sprintf( "
            INSERT INTO
                `%s`
            (
                %%s
            )
            VALUES
            (
                %%s
            )", $this->TABLE_NAME );
        
        $columns_string = '';
        $values = array();
        $values_string = '';
        
        foreach( $Data as $k=>$d )
        {
            $columns_string .= sprintf( "`%s`, ", $k );
            $values[] = $d;
            $values_string .= sprintf( "?, " );
        }
        
        $columns_string = substr( $columns_string, 0, -2 );
        $values_string = substr( $values_string, 0, -2 );
        
        $insert_sql = sprintf( $insert_sql, $columns_string, $values_string );
        
        $insert_result = $this->DB_LINK->Execute( $insert_sql, $values );
        
        if ( $this->DB_LINK->Insert_ID() )
        {
            return $this->DB_LINK->Insert_ID();
        }
        else
        {
            return false;
        }
            
    }

}

?>